package com.lzyyj.gmis.base.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Purview entity. @author MyEclipse Persistence Tools
 */

@SuppressWarnings("serial")
public class Purview  implements Serializable {

	// Fields

	private Integer id;
	private Purview parent;
	private Integer orderNo;
	private String name;
	private String description;
	private String purviewKey;
	private List<Purview> purviews = new ArrayList<Purview>(0);

	public List<Purview> getPurviews() {
		return purviews;
	}

	public void setPurviews(List<Purview> purviews) {
		this.purviews = purviews;
	}

	private Set<IRole> roles = new HashSet<IRole>(0);
	private Set<IOrganization> Organizations = new HashSet<IOrganization>(0);
	
	private Boolean authorized;
	
	public Boolean getAuthorized() {
		return authorized;
	}

	public void setAuthorized(Boolean authorized) {
		this.authorized = authorized;
	}

	/** default constructor */
	public Purview() {
	}

	/** minimal constructor */
	public Purview(Integer id) {
		this.id = id;
	}

	/** minimal constructor */
	public Purview(String name) {
		this.name = name;
	}
	
	public Purview(String name, String purviewKey) {
		this.name = name;
		this.purviewKey = purviewKey;
	}
	
	public Purview(String name, String purviewKey, String description) {
		this.name = name;
		this.purviewKey = purviewKey;
		this.description = description;
	}

	/** full constructor */
	public Purview(Purview parent, Integer orderNo, String name, String description,
			String purviewKey) {
		this.parent = parent;
		this.orderNo = orderNo;
		this.name = name;
		this.description = description;
		this.purviewKey = purviewKey;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Purview getParent() {
		return parent;
	}

	public void setParent(Purview parent) {
		this.parent = parent;
	}

	public Integer getOrderNo() {
		return this.orderNo;
	}

	public void setOrderNo(Integer orderNo) {
		this.orderNo = orderNo;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getPurviewKey() {
		return this.purviewKey;
	}

	public void setPurviewKey(String purviewKey) {
		this.purviewKey = purviewKey;
	}

	public Set<IRole> getRoles() {
		return roles;
	}

	public void setRoles(Set<IRole> roles) {
		this.roles = roles;
	}
	
	public Set<IOrganization> getOrganizations() {
		return Organizations;
	}

	public void setOrganizations(Set<IOrganization> organizations) {
		Organizations = organizations;
	}

	public List<Purview> getParentList(){
		List<Purview> parentList = new ArrayList<Purview>();
		if (parent!=null){
			for (Purview purview : parent.getParentList()){
				parentList.add(purview);
			}
			parentList.add(parent);
		}
		
		return parentList;
	}

	public Purview getModule() {
		Purview module = this;
		if (parent==null){
			module = this;
		}else{
			module = parent.getModule();
		}
		return module;
	}
	
	public int getLevel(){
		int level = 1;
		
		if (parent!=null){
			level = parent.getLevel()+1;
		}
		
		return level;
	}
	
	/**
	 * 功能权限下还有权限分类的话，返回当前有效值
	 * @return
	 */
	public Purview getRadioValue(){
		Purview value=null;
		
		if (parent!=null && purviews.size()>0){
			for (Purview purview:purviews){
				if (purview!=null&&purview.getAuthorized()!=null&&purview.getAuthorized()){
					if (value==null||value.getOrderNo()<purview.getOrderNo()){
						value = purview;
					}
				}
			}
		}
		
		return value;
	}
	
	/**
	 * 获取所有范围选项数量
	 * @return
	 */
	public int getAllRdoCount(){
		int count = 0;
		
		// 判断是否是功能权限
		for (Purview purview:purviews){
			count += purview.getPurviews().size();
		}
		
		return count;
	}
	
	/**
	 * 获取所有权限及范围显示格数量，权限占两格
	 * @return
	 */
	public int getAllPurviewAdnRangeCount(){
		int count = 0;
		
		switch (getLevel()) {
		case 1:
			count += 2;
			break;
		case 2:
			count += 2;
			break;
		case 3:
			count += 2;
			break;
		case 4:
			count += 2;
			break;
		case 5:
			count += 1;
			break;
		default:
			break;
		}
		
		// 判断是否是功能权限
		for (Purview purview:purviews){
			count += purview.getAllPurviewAdnRangeCount();
		}
		
		return count;
	}

	/**
	 * 获得所属模块Key
	 * @return
	 */
	public String getModuleKey() {
		String moduleKey;
		
		if (parent==null){
			moduleKey = purviewKey;
		}else{
			moduleKey = parent.getModuleKey();
		}
		
		return moduleKey;
	}

}